package com.rd.student.jiaji.class4.suyan.zrrd.day18;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

//import static com.zrrd.day18.Student.studentCount;

public class StudentTest {
    public static void main(String[] args) {
        // 添加学生对象到列表中
        LinkedList  list = new LinkedList();
        list.add(new Student("苏岩", "中加计算机", "Class 4", 18,"6",1));
        list.add(new Student("王宇芳", "中加计算机", "Class 1",  22,"66",2));
        list.add(new Student("黄一鸣", "中加计算机", "Class 2", 21,"666",3));
        list.add(new Student("李鑫", "中加计算机", "Class 1", 18,"6666",4));
        list.add(new Student("金洪宇", "中加计算机", "Class 1", 20,"6666",5));
        list.add(new Student("石睿", "中加计算机", "Class 2", 19,"6666",6));
        StudentTest s1=new StudentTest();
        s1.show(list);
        s1.removeStudent((LinkedList) list);
        s1.AddStudent(list);
    }
    //新增小组成员
    public LinkedList AddStudent(LinkedList list){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要新增学生姓名：");
        System.out.println("姓名：");
        String name=scanner.nextLine();
        //检查名字是否重复
        for(Object obj:list){
            Student student=(Student) obj;
            if (student.getName().equals(name)){
                System.out.println("插入失败，新成员名字重复");
            }
        }
        System.out.println("班级"+"例如：Class 2");
        String className= scanner.nextLine();
        System.out.println("专业：");
        String major=scanner.nextLine();
        System.out.println("年龄：");
        String describe=scanner.nextLine();
        System.out.println("描述：");
        int age =Integer.parseInt(scanner.nextLine());
        //自动生成学号
//        int studengId=studentCount +1;
//        studentCount++;
        //创建新成员
//        Student newStudeng=new Student(name,major,className,age,describe,studengId);
//        list.add(newStudeng);
        System.out.println("插入成功");
        return list;
    }
    //移除小组成员
    public LinkedList removeStudent(LinkedList list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要移除的信息");
        System.out.println("姓名");
        String input = scanner.nextLine();
        boolean found = false;
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Student student =(Student) iterator.next();
            //判断输入的名字，在列表中是否存在
            if (student.getName().equals(input)) {
                found = true;
                //判断这个班级的人数，如果人数为1程序跳到if（！found），不为1继续进行
            if (checkClass(student, list)) {
                System.out.println("暂时无法移除该成员");
                return (LinkedList) list;
            }
            iterator.remove();//删除当前指针所指元素
            System.out.println("成功移除成员" + student.getName());
            break;
        }
        }
        if (!found){//判断if语句的结果
            System.out.println("不存在");
        }
        return (LinkedList) list;
    }
    private boolean checkClass(Student student, List list){
        int count=0;
        for (Object obj:list){
            Student student1=(Student) obj;
            if (student1.getClassName().equals(student.getClassName())){
                count++;
            }
        }
        return count==1;
    }
public List show(List list){
        for (Object obj:list){
            Student student2=(Student)obj;
            System.out.println(student2);
        }
        return list;
}
}
